package com.anro.util

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.gyf.immersionbar.components.SimpleImmersionFragment
import java.lang.reflect.ParameterizedType

/**
 * Kotlin Fragment 基类
 * 实现 ViewBinding
 * 集成的项目需要在build.gradle中开启ViewBinding
 * 具体操作
 * android {
 *   ...
 *   buildFeatures {
 *      viewBinding true
 *   }
 * }
 *
 */
abstract class BaseKtFragment<T : ViewBinding> : SimpleImmersionFragment() {
    lateinit var binding: T

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val superclass = javaClass.genericSuperclass
        val aClass = (superclass as ParameterizedType).actualTypeArguments[0] as Class<*>
        val method = aClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
        binding = method.invoke(null, inflater) as T
        initView()
        initData()
        return binding.root
    }

    abstract fun initView()

    abstract fun initData()
}